package pe.com.ztaxi.taxis.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import pe.com.ztaxi.taxis.model.Zona;
import pe.com.ztaxi.taxis.service.ZonaService;
import pe.com.ztaxi.taxis.util.TaxiJsonUtil;

@PreAuthorize("hasAnyRole('ROLE_CONFIG', 'ROLE_ADMIN')")
@Controller
public class MantenerZonaController {

	@Autowired
	private ZonaService zonaService;
	
	@Autowired
	private TaxiJsonUtil jsonUtil;
	
	@RequestMapping(value = "/mantenerZonas", method = RequestMethod.GET)
	public String getMantenerZona(Model model) {
		List<Zona> zonas = zonaService.buscarTodas();		
		model.addAttribute("zonas",zonas);
		model.addAttribute("opcionMenu","MantenerZonas");
		return "mantenerZonas";
	}
	
	@RequestMapping(value = "/actualizarZona", method = RequestMethod.POST)
	public String actualizarZona(@RequestParam(value = "puntos") String puntos, @RequestParam(value = "nombreZona") String nombreZona, @RequestParam(value = "idZona") Integer idZona, Model model) {
		zonaService.actualizarPoligono(idZona, nombreZona, jsonUtil.prepararPuntos(puntos));
		return "redirect:/mantenerZonas";
	}
	
	@RequestMapping(value = "/agregarZona", method = RequestMethod.POST)
	public String agregarZona(@RequestParam(value = "puntos") String puntos, @RequestParam(value = "nombreZona") String nombreZona, Model model) {
		zonaService.registrarZona(nombreZona, jsonUtil.prepararPuntos(puntos));
		return "redirect:/mantenerZonas";
	}
}
